home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 276_01 / a684eval.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  11KB  |  448 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        6804 Cross-Assembler (Portable);
  4.     FILENAME:    A684EVAL.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6804 family microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All    assembler features are supported except
  12.             relocation linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6804;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program should compile on any full-featured C
  22.             compiler.  Subset compilers such as Toolworks C and
  23.             BDS C will present substantial difficulties."
  24.  
  25.     AUTHORS:    William C. Colley III;
  26. */
  27.  
  28. /*
  29.               6804 Cross-Assembler in Portable C
  30.  
  31.         Copyright (c) 1985, 1988 William C. Colley, III
  32.  
  33. Revision History:
  34.  
  35. Ver    Date        Description
  36.  
  37. 0.0    MAR 1988    Adapted from version 0.2 of the portable 6805 cross-
  38.             assembler which was adapted from version 3.2 of the
  39.             portable 6801 cross-assembler.  WCC3.
  40.  
  41. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  42.             into a VERY long loop if the user types a command line
  43.             like "A684 FILE.ASM -L".  WCC3 per Alex Cameron.
  44.  
  45. This file contains the assembler's expression evaluator and lexical analyzer.
  46. The lexical analyzer chops the input character stream up into discrete tokens
  47. that are processed by the expression analyzer and the line assembler.  The
  48. expression analyzer processes the token stream into unsigned results of
  49. arithmetic expressions.
  50. */
  51.  
  52. /*  Get global goodies:  */
  53.  
  54. #include "a684.h"
  55.  
  56. /*  Get access to global mailboxes defined in A684.C:            */
  57.  
  58. extern char line[];
  59. extern int filesp, forwd, pass;
  60. extern unsigned pc;
  61. extern FILE *filestk[], *source;
  62. extern TOKEN token;
  63.  
  64. /*  Expression analysis routine.  The token stream from the lexical    */
  65. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  66. /*  unsigned value.  If an error occurs during the evaluation, the    */
  67. /*  global flag    forwd is set to indicate to the line assembler that it    */
  68. /*  should not base certain decisions on the result of the evaluation.    */
  69.  
  70. static int bad;
  71.  
  72. unsigned expr()
  73. {
  74.     SCRATCH unsigned u;
  75.     unsigned eval();
  76.  
  77.     bad = FALSE;
  78.     u = eval(START);
  79.     return bad ? 0 : u;
  80. }
  81.  
  82. static unsigned eval(pre)
  83. unsigned pre;
  84. {
  85.     register unsigned op, u, v;
  86.     TOKEN *lex();
  87.     void exp_error(), unlex();
  88.  
  89.     for (;;) {
  90.     u = op = lex() -> valu;
  91.     switch (token.attr & TYPE) {
  92.         case REG:
  93.         case IMM:    exp_error('S');  break;
  94.  
  95.         case SEP:    if (pre != START) unlex();
  96.         case EOL:    exp_error('E');  return;
  97.  
  98.         case OPR:    if (!(token.attr & UNARY)) { exp_error('E');  break; }
  99.             u = (op == '*' ? pc :
  100.                 eval((op == '+' || op == '-') ?
  101.                 (unsigned) UOP1 : token.attr & PREC));
  102.             switch (op) {
  103.                 case '-':    u = word(-u);  break;
  104.  
  105.                 case NOT:    u ^= 0xffff;  break;
  106.  
  107.                 case HIGH:    u = high(u);  break;
  108.  
  109.                 case LOW:    u = low(u);  break;
  110.             }
  111.  
  112.         case VAL:    
  113.         case STR:    for (;;) {
  114.                 op = lex() -> valu;
  115.                 switch (token.attr & TYPE) {
  116.                 case REG:
  117.                 case IMM:   exp_error('S');  break;
  118.  
  119.                 case SEP:   if (pre != START) unlex();
  120.                 case EOL:   if (pre == LPREN) exp_error('(');
  121.                         return u;
  122.  
  123.                 case STR:
  124.                 case VAL:   exp_error('E');  break;
  125.  
  126.                 case OPR:   if (!(token.attr & BINARY)) {
  127.                         exp_error('E');  break;
  128.                         }
  129.                         if ((token.attr & PREC) >= pre) {
  130.                         unlex();  return u;
  131.                         }
  132.                         if (op != ')')
  133.                         v = eval(token.attr & PREC);
  134.                         switch (op) {
  135.                         case '+':   u += v;  break;
  136.  
  137.                         case '-':   u -= v;  break;
  138.  
  139.                         case '*':   u *= v;  break;
  140.  
  141.                         case '/':   u /= v;  break;
  142.  
  143.                         case MOD:   u %= v;  break;
  144.  
  145.                         case AND:   u &= v;  break;
  146.  
  147.                         case OR:    u |= v;  break;
  148.  
  149.                         case XOR:   u ^= v;  break;
  150.  
  151.                         case '<':   u = u < v;  break;
  152.  
  153.                         case LE:    u = u <= v;  break;
  154.  
  155.                         case '=':   u = u == v;  break;
  156.  
  157.                         case GE:    u = u >= v;  break;
  158.  
  159.                         case '>':   u = u > v;  break;
  160.  
  161.                         case NE:    u = u != v;  break;
  162.  
  163.                         case SHL:   if (v > 15)
  164.                                 exp_error('E');
  165.                                 else u <<= v;
  166.                                 break;
  167.  
  168.                         case SHR:   if (v > 15)
  169.                                 exp_error('E');
  170.                                 else u >>= v;
  171.                                 break;
  172.  
  173.                         case ')':   if (pre == LPREN)
  174.                                 return u;
  175.                                 exp_error('(');
  176.                                 break;
  177.                         }
  178.                         clamp(u);
  179.                         break;
  180.                 }
  181.             }
  182.             break;
  183.     }
  184.     }
  185. }
  186.  
  187. static void exp_error(c)
  188. char c;
  189. {
  190.     forwd = bad = TRUE;  error(c);
  191. }
  192.  
  193. /*  Lexical analyzer.  The source input character stream is chopped up    */
  194. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  195. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  196. /*  to an attribute word, a numeric value, and (possibly) a string    */
  197. /*  value.                                */
  198.  
  199. static int oldt = FALSE;
  200. static int quote = FALSE;
  201.  
  202. TOKEN *lex()
  203. {
  204.     SCRATCH char c, *p;
  205.     SCRATCH unsigned b;
  206.     SCRATCH OPCODE *o;
  207.     SCRATCH SYMBOL *s;
  208.     OPCODE *find_operator();
  209.     SYMBOL *find_symbol();
  210.     void exp_error(), make_number(), pops(), pushc(), trash();
  211.  
  212.     if (oldt) { oldt = FALSE;  return &token; }
  213.     trash();
  214.     if (isalph(c = popc())) {
  215.     pushc(c);  pops(token.sval);
  216.     if (o = find_operator(token.sval)) {
  217.         token.attr = o -> attr;
  218.         token.valu = o -> valu;
  219.     }
  220.     else {
  221.         token.attr = VAL;  token.valu = 0;
  222.         if (s = find_symbol(token.sval)) {
  223.         token.valu = s -> valu;
  224.         if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  225.         }
  226.         else exp_error('U');
  227.     }
  228.     }
  229.     else if (isnum(c)) {
  230.     pushc(c);  pops(token.sval);
  231.     for (p = token.sval; *p; ++p);
  232.     switch (toupper(*--p)) {
  233.         case 'B':    b = 2;  break;
  234.  
  235.         case 'O':
  236.         case 'Q':    b = 8;  break;
  237.  
  238.         default:    ++p;
  239.         case 'D':    b = 10;  break;
  240.  
  241.         case 'H':    b = 16;  break;
  242.     }
  243.     *p = '\0';  make_number(b);
  244.     }
  245.     else switch (c) {
  246.     case '%':   b = 2;  goto num;
  247.  
  248.     case '@':   b = 8;  goto num;
  249.  
  250.     case '$':   b = 16;
  251. num:            pops(token.sval);
  252.             make_number(b);
  253.             break;
  254.  
  255.     case '#':   token.attr = IMM;
  256.             break;
  257.  
  258.     case '(':   token.attr = UNARY + LPREN + OPR;
  259.             goto opr1;
  260.  
  261.     case ')':   token.attr = BINARY + RPREN + OPR;
  262.             goto opr1;
  263.  
  264.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;
  265.             goto opr1;
  266.  
  267.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;
  268.             goto opr1;
  269.  
  270.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;
  271.             goto opr1;
  272.  
  273.     case '/':   token.attr = BINARY + MULT + OPR;
  274. opr1:            token.valu = c;
  275.             break;
  276.  
  277.     case '<':   token.valu = c;
  278.             if ((c = popc()) == '=') token.valu = LE;
  279.             else if (c == '>') token.valu = NE;
  280.             else pushc(c);
  281.             goto opr2;
  282.  
  283.     case '=':   token.valu = c;
  284.             if ((c = popc()) == '<') token.valu = LE;
  285.             else if (c == '>') token.valu = GE;
  286.             else pushc(c);
  287.             goto opr2;
  288.  
  289.     case '>':   token.valu = c;
  290.             if ((c = popc()) == '<') token.valu = NE;
  291.             else if (c == '=') token.valu = GE;
  292.             else pushc(c);
  293. opr2:            token.attr = BINARY + RELAT + OPR;
  294.             break;
  295.  
  296.     case '\'':
  297.     case '"':   quote = TRUE;  token.attr = STR;
  298.             for (p = token.sval; (*p = popc()) != c; ++p)
  299.             if (*p == '\n') { exp_error('"');  break; }
  300.             *p = '\0';  quote = FALSE;
  301.             if ((token.valu = token.sval[0]) && token.sval[1])
  302.             token.valu = (token.valu << 8) + token.sval[1];
  303.             break;
  304.  
  305.     case ',':   token.attr = SEP;
  306.             break;
  307.  
  308.         case '\n':  token.attr = EOL;
  309.             break;
  310.     }
  311.     return &token;
  312. }
  313.  
  314. static void make_number(base)
  315. unsigned base;
  316. {
  317.     SCRATCH char *p;
  318.     SCRATCH unsigned d;
  319.     void exp_error();
  320.  
  321.     token.attr = VAL;
  322.     token.valu = 0;
  323.     for (p = token.sval; *p; ++p) {
  324.     d = toupper(*p) - (isnum(*p) ? '0' : 'A' - 10);
  325.     token.valu = token.valu * base + d;
  326.     if (!ishex(*p) || d >= base) {